//package com.example.springsecurityoauthdemo.handler;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.http.HttpStatus;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author YCL
// * @version 1.0
// * @Description
// * @date 2021/9/30
// */
//@Component
//public class MyAuthenticationFailHandler extends SimpleUrlAuthenticationFailureHandler {
//
//    /**
//     * json 转换工具类
//     */
//    private ObjectMapper objectMapper;
//
//    @Override
//    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
//        //super.onAuthenticationFailure(request, response, exception);
//
//
//        String msg = null;
//        if (exception instanceof BadCredentialsException) {
//            msg = "密码错误";
//        } else {
//            msg = exception.getMessage();
//        }
//
//        Map<String, String> rsp = new HashMap<>();
//        response.setStatus(HttpStatus.UNAUTHORIZED.value());
//        rsp.put("code", HttpStatus.UNAUTHORIZED.value() + "");
//        rsp.put("msg", msg);
//        response.setContentType("application/json;charset=UTF-8");
//        response.getWriter().write(objectMapper.writeValueAsString(rsp));
//        response.getWriter().flush();
//        response.getWriter().close();
//    }
//}
